package com.alavente.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.alavente.objects.Client;
import com.alavente.objects.DemandeVente;
import com.alavente.service.DemandeVenteManager;

public class DemandeVenteForm extends ActionForm {

	private String method;
	private int numDemande;
	private int numUser;
	private int numSsCategorie;
	private int numCategorie;
	private String titre;
	private String descript;
	private Float prix;
	
	@Override
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
		
		if (titre.isEmpty()) {
			errors.add("titre", new ActionMessage("error.titre.required"));
		}
		
		if (descript.isEmpty()) {
			errors.add("description", new ActionMessage("error.description.required"));
		}
		
		if (numCategorie == 0) {
			errors.add("categorie", new ActionMessage("error.categorie.required"));
		}

		if (numSsCategorie == 0) {
			errors.add("ssCategorie", new ActionMessage("error.ssCategorie.required"));
		}
		
		if (prix == null) {
			errors.add("prix", new ActionMessage("error.prix.required"));
		}
		
		return errors;
	}
	
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {

		super.reset(mapping, request);
		String method = request.getParameter("method");

		if (method.equals("update")) {
			int numDemande = 0;
			try {
				numDemande = Integer.parseInt(request.getParameter("id"));
			}
			catch (NumberFormatException e){}
			
			int numClient = ((Client)request.getSession().getAttribute("client")).getNumClient();
			DemandeVente demandeVente = new DemandeVenteManager().getDemandeVente(numDemande, numClient);
			
			if (demandeVente != null) {			
				this.numDemande = numDemande;
				numSsCategorie = demandeVente.getSsCategorie().getNumSsCat();
				numCategorie = demandeVente.getCategorie().getNumCat();
				titre = demandeVente.getTitre();
				descript = demandeVente.getDescript();
				prix = demandeVente.getPrix();	
			}
		}
	}
	
	public int getNumDemande() {
		return numDemande;
	}
	public void setNumDemande(int numDemande) {
		this.numDemande = numDemande;
	}

	public String getTitre() {
		return titre;
	}
	public void setTitre(String titre) {
		this.titre = titre;
	}

	public String getDescript() {
		return descript;
	}
	public void setDescript(String descript) {
		this.descript = descript;
	}

	public Float getPrix() {
		return prix;
	}
	public void setPrix(Float prix) {
		this.prix = prix;
	}

	public void setNumSsCategorie(int numSsCategorie) {
		this.numSsCategorie = numSsCategorie;
	}
	public int getNumSsCategorie() {
		return numSsCategorie;
	}

	public void setNumCategorie(int numCategorie) {
		this.numCategorie = numCategorie;
	}
	public int getNumCategorie() {
		return numCategorie;
	}

	public void setNumUser(int numUser) {
		this.numUser = numUser;
	}

	public int getNumUser() {
		return numUser;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	private static final long serialVersionUID = 1L;	
}
